package cn.burnit.service;

import cn.burnit.pojo.Reader;
import cn.burnit.utils.ConnectUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 读者jdbc方法
 */
public class ReaderService {



    /**
     * 添加读者
     * @param reader
     * @return
     */
      public int addReader(Reader reader) {
        int i=0;
        ConnectUtils.getDriver();
        Connection connection=null;
        try {
            connection=ConnectUtils.getConnection();
            String addReaderSql="insert into reader values (null,?,?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(addReaderSql);
            ps.setString(1,reader.getReadername());
            ps.setString(2,reader.getReadertype());
            ps.setString(3,reader.getSex());
            ps.setInt(4,reader.getMax_num());
            ps.setInt(5,reader.getDays_num());
            i=ps.executeUpdate();
            if (i>0){
                System.out.println("借阅者个人信息添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 通过读者名称查找Reader对象
     * @param readername
     * @return
     */
    public Reader findReaderByReadername(String readername) {
          ConnectUtils.getDriver();
          Connection connection=null;
        try {
            connection=ConnectUtils.getConnection();
            String findReaderSql="select * from reader where readername=?";
            PreparedStatement ps = connection.prepareStatement(findReaderSql);
            ps.setString(1,readername);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()){
                Reader reader = new Reader();
                readername=resultSet.getString(1);
                reader.setReadername(readername);
                return reader;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public Reader serch(String readername) {
        ConnectUtils.getDriver();
        Connection connection=null;
        try {
            connection=ConnectUtils.getConnection();
            String serachReaderSql="select * from reader where readername=?";
            PreparedStatement ps = connection.prepareStatement(serachReaderSql);
            ps.setString(1,readername);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                Reader reader=new Reader();
                reader.setId(rs.getInt(1));
                reader.setReadername(rs.getString(2));
                reader.setReadertype(rs.getString(3));
                reader.setSex(rs.getString(4));
                reader.setMax_num(rs.getInt(5));
                reader.setDays_num(rs.getInt(6));
                return reader;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查找所有读者
     * @return
     */
    public List<Reader> list() {
        ConnectUtils.getDriver();
        Connection connection=null;
        List<Reader> list= new ArrayList<>();
        try {
            connection=ConnectUtils.getConnection();
            String getAllSql="select * from reader";
            PreparedStatement ps = connection.prepareStatement(getAllSql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                Reader reader = new Reader();
                reader.setId(rs.getInt(1));
                reader.setReadername(rs.getString(2));
                reader.setReadertype(rs.getString(3));
                reader.setSex(rs.getString(4));
                reader.setMax_num(rs.getInt(5));
                reader.setDays_num(rs.getInt(6));
                list.add(reader);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 更新读者
     * @param reader
     * @return
     */
    public int updateReader(Reader reader) {
        int i=0;
        ConnectUtils.getDriver();
        Connection connection=null;
        try {
            connection=ConnectUtils.getConnection();
            String updateReaderSql="update reader set readername=?,readertype=?,sex=?,max_num=?,days_num=? where id=?";
            PreparedStatement ps = connection.prepareStatement(updateReaderSql);

            ps.setString(1,reader.getReadername());
            ps.setString(2,reader.getReadertype());
            ps.setString(3,reader.getSex());
            ps.setInt(4,reader.getMax_num());
            ps.setInt(5,reader.getDays_num());
            ps.setInt(6,reader.getId());
            i=ps.executeUpdate();
            if (i>0){
                System.out.println("修改成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    public int updateReaderMaxnum(Reader reader) {
        int i=0;
        ConnectUtils.getDriver();
        Connection connection=null;
        try {
            connection=ConnectUtils.getConnection();
            String updateReaderSql="update reader set max_num=? where id=?";
            PreparedStatement ps = connection.prepareStatement(updateReaderSql);
            ps.setInt(1,reader.getMax_num());
            ps.setInt(2,reader.getId());
            i=ps.executeUpdate();
            if (i>0){
                System.out.println("修改成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
